package com.example.demo.Util;

import com.example.demo.entity.*;
import com.example.demo.entity.Module;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Component
public class UtilBean {

    private static UtilBean utilBean;

    private static final String key="key";

    private static final String cardKey="cardKey";

    private final String studentKey="studentKey";

    private Integer studentSeq = 1000;

    private Integer cardSeq = 10;

    private char studentLabelSeq = 'a';

    private Map<String,Student> studentMap = new HashMap<>();

    private List<Module> modules = new ArrayList<>();

    private List<String> supervisorName = new ArrayList<>();

    private UtilBean(){

    }

    public static UtilBean getInstance(){
        //第一重判断，先判断实例是否存在，不存在再加锁处理
        if (utilBean == null)
        {
            //加锁的程序在某一时刻只允许一个线程访问
            synchronized(key)
            {
                //第二重判断
                if(utilBean==null)
                {
                    utilBean = new UtilBean();  //创建单例实例
                }
            }
        }
        return utilBean;
    }

    public List<Module> getModules() {
        String fileName = System.getProperty("user.dir")+"\\Modules.txt";

        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = null;
        try {
            lines = Files.lines(Paths.get(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 随机行顺序进行数据处理
        lines.forEach(ele -> {
            Module module = new Module();
            String[] split = ele.split(",");
            module.setModuleCode(split[0]);
            module.setModuleName(split[1]);
            module.setCredits(split[2]);
            modules.add(module);
        });
        return modules;
    }

    public Integer getStudentSeq() {
        synchronized(key) {
            if (studentSeq == 9999) {
                studentSeq = 1000;
            }
            return studentSeq++;
        }
    }

    public Integer getCardSeq() {
        //加锁
        synchronized(cardKey) {
            if (cardSeq == 99) {
                cardSeq = 10;
            }
            return cardSeq++;
        }
    }

    public char getStudentLabelSeq() {
        synchronized(key) {
            if (studentSeq == 9999 && studentLabelSeq+1<123) {
                studentLabelSeq += 1;
            }
            return studentLabelSeq;
        }
    }

    public Map<String, Student> getStudentMap() {
        return studentMap;
    }

    public void addStudentMap(String studentId,Student student) {
        synchronized(studentKey) {
            studentMap.put(studentId,student);
        }
    }

    public List<String> getSupervisorName() {
        String fileName = System.getProperty("user.dir")+"\\supervisorName.txt";

        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = null;
        try {
            lines = Files.lines(Paths.get(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 随机行顺序进行数据处理
        lines.forEach(ele -> {
            supervisorName.add(ele);
        });
        return supervisorName;
    }


}
